package voidgame.core;
import java.util.ArrayList;
import java.util.Random;

public class StarSystem{
	
	ArrayList<Planet> planets;
	ArrayList<Wormhole> wormholes;
		
	Cell[][] map;
	
	Galaxy galaxy;
	
	Star star;
	
	String name;
	
	
	public StarSystem(Galaxy g){
		
		planets = new ArrayList<Planet>();
		wormholes = new ArrayList<Wormhole>();
		
		map = new Cell[55][25];
		
		galaxy = g;
		
		star = new Star(galaxy.getAstralName());
		name = star.name;
				
		int noplanets = new Random().nextInt(10)+1;
		
		int dist=0;
		
		for(int x =0; x<noplanets; x++){
			dist++;
			if(dist+(noplanets-x)<10&&new Random().nextInt(2)==0)
				dist=dist+(int)((10-noplanets)/2);
			
			if(new Random().nextInt(10)==0)
				planets.add(new Planet(galaxy.getAstralName(),dist,this));
			else	
				planets.add(new Planet(name + " " + (x+1),dist,this));
		}
		
		genMap();
		
	}
	
	public void makeSol(){
		
		planets = new ArrayList<Planet>();
		name = "Sol";
		
		star.name="Sol";
		star.size=1;
		star.temp=1;
		star.startype=2;
		star.sprite=".//images//celestial//star//star01.png";
		star.genDesc();
		
		Planet mercury = new Planet("Mercury",1,this);
		mercury.size=0;
		mercury.type=0;
		mercury.rings=0;
		mercury.life=false;
		mercury.moons = new ArrayList<Moon>();
		mercury.sprite = ".//images//celestial//sol//mercury.png";
		mercury.genDesc();
		planets.add(mercury);
		
		Planet venus = new Planet("Venus",2,this);
		venus.size=1;
		venus.type=0;
		venus.rings=0;
		venus.life=false;
		venus.moons = new ArrayList<Moon>();
		venus.sprite = ".//images//celestial//sol//venus.png";
		venus.genDesc();
		planets.add(venus);
		
		Planet earth = new Planet("Earth",3,this);
		earth.size=2;
		earth.type=0;
		earth.rings=0;
		earth.temp=2;
		earth.life=true;
		earth.moons = new ArrayList<Moon>();
		Moon theMoon = new Moon("The Moon", 1, earth);
		theMoon.name = "The Moon";
		theMoon.size=1;
		theMoon.sprite = ".//images//celestial//sol//themoon.png";
		theMoon.genDesc();
		earth.moons.add(theMoon);
		earth.sprite = ".//images//celestial//sol//earth.png";
		earth.genDesc();
		planets.add(earth);
		
		Planet mars = new Planet("Mars",4,this);
		mars.size=2;
		mars.type=0;
		mars.rings=0;
		mars.temp=2;
		mars.life=false;
		mars.moons = new ArrayList<Moon>();
		Moon phobos = new Moon("Phobos", 1, mars);
		phobos.name = "Phobos";
		phobos.size=0;
		mars.moons.add(phobos);
		phobos.genDesc();
		Moon deimos = new Moon("Deimos", 2, mars);
		deimos.name = "Deimos";
		deimos.size=0;
		deimos.genDesc();
		mars.moons.add(deimos);
		mars.sprite = ".//images//celestial//sol//mars.png";
		mars.genDesc();
		planets.add(mars);
		
		Planet jupiter = new Planet("Jupiter",6,this);
		jupiter.size=6;
		jupiter.type=1;
		jupiter.rings=0;
		jupiter.temp=3;
		jupiter.life=false;
		jupiter.moons = new ArrayList<Moon>();
		Moon amalthea = new Moon("Amalthea", 1, jupiter);
		amalthea.name = "Amalthea";
		amalthea.size=0;
		amalthea.genDesc();
		jupiter.moons.add(amalthea);
		Moon io = new Moon("Io", 2, jupiter);
		io.name = "Io";
		io.size=1;
		io.genDesc();
		jupiter.moons.add(io);
		Moon europa = new Moon("Europa", 3, jupiter);
		europa.name = "Europa";
		europa.size=1;
		europa.genDesc();
		jupiter.moons.add(europa);
		Moon ganymede = new Moon("Ganymede", 4, jupiter);
		ganymede.name = "Ganymede";
		ganymede.size=2;
		ganymede.genDesc();
		jupiter.moons.add(ganymede);
		Moon callisto = new Moon("Callisto", 5, jupiter);
		callisto.name = "Callisto";
		callisto.size=2;
		callisto.genDesc();
		jupiter.moons.add(callisto);
		Moon themisto = new Moon("Themisto", 6, jupiter);
		themisto.name = "Themisto";
		themisto.size=0;
		themisto.genDesc();
		jupiter.moons.add(themisto);
		Moon himalia = new Moon("Himalia", 7, jupiter);
		himalia.name = "Himalia";
		himalia.size=0;
		himalia.genDesc();
		jupiter.moons.add(himalia);
		Moon elara = new Moon("Elara", 8, jupiter);
		elara.name = "Elara";
		elara.size=0;
		elara.genDesc();
		jupiter.moons.add(elara);
		jupiter.sprite = ".//images//celestial//sol//jupiter.png";
		jupiter.genDesc();
		planets.add(jupiter);
		
		Planet saturn = new Planet("Saturn",7,this);
		saturn.size=6;
		saturn.type=1;
		saturn.rings=2;
		saturn.temp=3;
		saturn.life=false;
		saturn.moons = new ArrayList<Moon>();
		Moon enceladus = new Moon("Enceladus", 1, saturn);
		enceladus.name = "Enceladus";
		enceladus.size=0;
		enceladus.genDesc();
		saturn.moons.add(enceladus);
		Moon tethys = new Moon("Tethys", 2, saturn);
		tethys.name = "Tethys";
		tethys.size=0;
		tethys.genDesc();
		saturn.moons.add(tethys);
		Moon dione = new Moon("Dione", 3, saturn);
		dione.name = "Dione";
		dione.size=0;
		dione.genDesc();
		saturn.moons.add(dione);
		Moon rhea = new Moon("Rhea", 4, saturn);
		rhea.name = "Rhea";
		rhea.size=0;
		rhea.genDesc();
		saturn.moons.add(rhea);
		Moon titan = new Moon("Titan", 5, saturn);
		titan.name = "Titan";
		titan.size=2;
		titan.genDesc();
		saturn.moons.add(titan);
		Moon iapetus = new Moon("Iapetus", 6, saturn);
		iapetus.name = "Iapetus";
		iapetus.size=0;
		iapetus.genDesc();
		saturn.moons.add(iapetus);
		saturn.sprite = ".//images//celestial//sol//saturn.png";
		saturn.genDesc();
		planets.add(saturn);
		
		Planet uranus = new Planet("Uranus",8,this);
		uranus.size=5;
		uranus.type=1;
		uranus.rings=1;
		uranus.temp=3;
		uranus.life=false;
		uranus.moons = new ArrayList<Moon>();
		Moon miranda = new Moon("Miranda", 1, uranus);
		miranda.name = "Miranda";
		miranda.size=0;
		miranda.genDesc();
		uranus.moons.add(miranda);
		Moon ariel = new Moon("Ariel", 2, uranus);
		ariel.name = "Ariel";
		ariel.size=0;
		ariel.genDesc();
		uranus.moons.add(ariel);
		Moon umbriel = new Moon("Umbriel", 3, uranus);
		umbriel.name = "Umbriel";
		umbriel.size=0;
		umbriel.genDesc();
		uranus.moons.add(umbriel);
		Moon titania = new Moon("Titania", 4, uranus);
		titania.name = "Titania";
		titania.size=1;
		titania.genDesc();
		uranus.moons.add(titania);
		Moon oberon = new Moon("Oberon", 5, uranus);
		oberon.name = "Oberon";
		oberon.size=1;
		oberon.genDesc();
		uranus.moons.add(oberon);
		uranus.sprite = ".//images//celestial//sol//uranus.png";
		uranus.genDesc();
		planets.add(uranus);
		
		Planet neptune = new Planet("Neptune",9,this);
		neptune.size=5;
		neptune.type=1;
		neptune.rings=0;
		neptune.temp=4;
		neptune.life=false;
		neptune.moons = new ArrayList<Moon>();
		Moon proteus = new Moon("Proteus", 1, neptune);
		proteus.name = "Proteus";
		proteus.size=0;
		proteus.genDesc();
		neptune.moons.add(proteus);
		Moon triton = new Moon("Triton", 2, neptune);
		triton.name = "Triton";
		triton.size=1;
		triton.genDesc();
		neptune.moons.add(triton);
		Moon nerid = new Moon("Nerid", 3, neptune);
		nerid.name = "Nerid";
		nerid.size=0;
		nerid.genDesc();
		neptune.moons.add(nerid);
		neptune.sprite = ".//images//celestial//sol//neptune.png";
		neptune.genDesc();
		planets.add(neptune);
		
		genMap();		
	}
	
	public void addWormHole(){
		StarSystem candidate = galaxy.systems.get(new Random().nextInt(galaxy.systems.size()));
		
		boolean candidateCycleCheck=false;
		for(Wormhole w:candidate.wormholes){
			if(w.alpha==this||w.beta==this)
				candidateCycleCheck=true;
		}
		
		while(candidate.wormholes.size()>2||candidateCycleCheck||(candidate.wormholes.size()>1&&new Random().nextInt(2)==0)||candidate==this){
			candidate = galaxy.systems.get(new Random().nextInt(galaxy.systems.size()));
			
			candidateCycleCheck=false;
			for(Wormhole w:candidate.wormholes){
				if(w.alpha==this||w.beta==this)
					candidateCycleCheck=true;
			}
		
		}
		
				
		Wormhole hole = new Wormhole(this, candidate);
		wormholes.add(hole);
		candidate.wormholes.add(hole);
		
		boolean connectedCheck = true;
		for(StarSystem ss:galaxy.systems){
			if(ss.wormholes.size()==0)
				connectedCheck=false;
		}
		
		//Hilariously inefficient!
		genMap();
			
		if(!connectedCheck)
			candidate.addWormHole();
		
	}
	
	
	public void genMap(){
		for(int x = 0; x<55; x++){
			for(int y=0;y<25;y++){
				if(x<star.size||(x<star.size+1&&y>4&&y<20)||(x<star.size+2&&y>9&&y<15))
					map[x][y]=new Cell(1, star);
				else
					map[x][y]=new Cell();
			}
		}
		
		for(Planet p:planets){
			p.pos[0]=(p.dist*5)+3;
			p.pos[1]=(new Random().nextInt(15)+5);
			map[p.pos[0]][p.pos[1]]=new Cell(2,p);
			
			for(Moon m:p.moons){
				switch(m.dist){
				case 1: m.pos[0]=p.pos[0]-1; m.pos[1]=p.pos[1]-1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 2: m.pos[0]=p.pos[0]+1; m.pos[1]=p.pos[1]+1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 3: m.pos[0]=p.pos[0]-1; m.pos[1]=p.pos[1]+1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 4: m.pos[0]=p.pos[0]+1; m.pos[1]=p.pos[1]-1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 5: m.pos[0]=p.pos[0]; m.pos[1]=p.pos[1]-1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 6: m.pos[0]=p.pos[0]; m.pos[1]=p.pos[1]+1; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 7: m.pos[0]=p.pos[0]-1; m.pos[1]=p.pos[1]; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				case 8: m.pos[0]=p.pos[0]+1; m.pos[1]=p.pos[1]; map[m.pos[0]][m.pos[1]]=new Cell(3,m); break;
				}
			}
		}
		
		for(Wormhole w:wormholes){
			w.pos[0]=(new Random().nextInt(50))+5;
			w.pos[1]=(new Random().nextInt(10));
			if(w.pos[1]>4)
				w.pos[1]+=15;
			
			while(map[w.pos[0]][w.pos[1]].type!=0){
				w.pos[0]=(new Random().nextInt(50))+5;
				w.pos[1]=(new Random().nextInt(10));
				if(w.pos[1]>4)
					w.pos[1]+=15;
			}
				
			map[w.pos[0]][w.pos[1]]=new Cell(4,w);
			if(w.alpha==this)
				w.alphaPosSet();
			else
				w.betaPosSet();
		}
		
		
	}
	
}